package cl.android.mochileros;




import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

@SuppressWarnings("deprecation")
public class MapActivity extends FragmentActivity  {
	private DrawerLayout drawerLayout;
	private ListView listView;
	private ActionBarDrawerToggle drawerToggle;
	
	private CharSequence drawerTitle;
	private CharSequence title;
	private String [] sections;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		
		title = drawerTitle = getTitle();
		sections = getResources().getStringArray(R.array.secciones);
		drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
		listView = (ListView) findViewById(R.id.drawerList);
		

		drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
		listView.setAdapter(new ArrayAdapter<String>(this, 
				R.layout.drawer_list_item,sections));
		listView.setOnItemClickListener(new DrawerItemClickListener());

		// enable ActionBar app icon to behave as action to toggle nav drawer
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		
		
		drawerToggle = new ActionBarDrawerToggle(
				this, drawerLayout, 
				R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close){
			@Override
			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(drawerTitle);
				invalidateOptionsMenu();
			}
			@Override
			public void onDrawerClosed(View drawerView) {
				getActionBar().setTitle(title);
				invalidateOptionsMenu();
			}
		};
		drawerLayout.setDrawerListener(drawerToggle);
		
		if (savedInstanceState == null) {
			selectItem(0);
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.map, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	/* Called whenever we call invalidateOptionsMenu() */
	@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
//        boolean drawerOpen = drawerLayout.isDrawerOpen(listView);
//        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(drawerToggle.onOptionsItemSelected(item)){
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	 /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }
    
    
	public void selectItem(int position){
		switch (position) {
		case 0:
			cargarFragment(new CategoriasFragment());
			break;
		case 1:
			cargarFragment(new MapaFragment());
			break;
		case 2: 
			cargarFragment(new RecomendadosFragment());
			break;
		case 3: 
			cargarFragment(new ProfileFragment());
		default:
			break;
		}
		listView.setItemChecked(position, true);
		setTitle(sections[position]);
		drawerLayout.closeDrawer(listView);
	}
	
	public void cargarFragment(Fragment fragment){
		FragmentManager manager = getFragmentManager();
		manager.beginTransaction().replace(R.id.mainContent, fragment, "fragmento").commit();
	}
	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if( this.getFragmentManager().getBackStackEntryCount() != 0 ){
                this.getFragmentManager().popBackStack();
                return true;
            }
            // If there are no fragments on stack perform the original back button event
        }

        return super.onKeyDown(keyCode, event);
    }
	
	@Override
	public void setTitle(CharSequence title){
		this.title = title;
		getActionBar().setTitle(this.title);
	}
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		drawerToggle.syncState();
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		drawerToggle.onConfigurationChanged(newConfig);
	}
	

//	@Override
//	public void onItemClick(AdapterView<?> parent, View view, int position,
//			long id) {
//		Toast.makeText(this, sections[position], Toast.LENGTH_SHORT).show();
//		selectItem(position);
//	}
	
	
	
	
	

	

	
}
